package sn.gandal.agriculture.agrimobile.j2me.gui;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStoreException;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.Adresse;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.BusinessServicesImpl;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.IBusinessServices;

/**
 *
 * @author dysow
 */
public class ConfigurationServeurForm extends Form implements CommandListener
{

    private Display display;
    private Displayable previousScreen;
    private TextField txtAdresse;
    private Command cmdBack;
    private Command cmdSave;
    private static final int SAUVEGARDE_SUCCESS = 1;

    public ConfigurationServeurForm(Display display, Displayable previous)
    {
	super("Changer l'adresse");

	this.display = display;
	this.previousScreen = previous;

	this.initialiseComponents();
	this.initialiseCommands();
	this.addComponents();
	this.addCommands();

	this.setCommandListener(this);
    }

    private void initialiseComponents()
    {
	this.txtAdresse = new TextField("Adresse du serveur", "http://", 200, TextField.ANY);
    }

    private void initialiseCommands()
    {
	this.cmdBack = new Command("Back", Command.BACK, 1);
	this.cmdSave = new Command("Sauvegarder", Command.OK, 2);
    }

    private void addComponents()
    {
	this.append(this.txtAdresse);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdBack);
	this.addCommand(this.cmdSave);
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdBack))
	{
	    this.backAction();
	}
	else if (c.equals(this.cmdSave))
	{
	    this.saveAction();
	}
    }

    private void backAction()
    {
	this.display.setCurrent(this.previousScreen);
    }

    private void saveAction()
    {
	try
	{
	    Adresse adresse = new Adresse(this.txtAdresse.getString());

	    ControleFormsUtil util = new ControleFormsUtil();
	    String erreur = util.controleConfigurationServeur(adresse);

	    if (erreur.length() > 0)
	    {
		Alert alert = new Alert("Erreur", "L'adresse n'est pas valide", null, AlertType.ERROR);
		this.display.setCurrent(alert);
	    }
	    else
	    {
		IBusinessServices businessServices = BusinessServicesImpl.getInstance();
		businessServices.saveWebAdresse(adresse);

		String succes ="L'enregistrement de l'adresse du serveur a bien été pris en compte !";
		//confirmation.setDetails("Information", "Enregistrement effectué. Nouvelle adresse : " + adresse.getWebAddress(), null, AlertType.INFO);
		this.display.setCurrent(ConfirmationForm.ConfirmAction(succes),previousScreen);

//		this.display.setCurrent(this.previousScreen);
	    }
	}
	catch (RecordStoreException ex)
	{
	    ex.printStackTrace();
	}
	catch (IOException ex)
	{
	    ex.printStackTrace();
	}
    }

    public void okAction(int action)
    {
	if(action == ConfigurationServeurForm.SAUVEGARDE_SUCCESS)
	{
	    this.display.setCurrent(this.previousScreen);
	}
    }
}
